package com.xbb.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class InterceptorProducer {
  public static void main(String[] args) {
    Properties prop = new Properties();
    prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop1:9092");
    prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
    prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
    prop.put(ProducerConfig.ACKS_CONFIG, "all");

    List<String> interceptors = Arrays.asList("com.xbb.kafka.Interceptor.AddTimeInterceptor", "com.xbb.kafka.Interceptor.CounterInterceptor");
    prop.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, interceptors);

    KafkaProducer<String, String> producer = new KafkaProducer<>(prop);

    for (int i = 0; i < 10; i++) {
      producer.send(new ProducerRecord<>(
              "top",
              "xbb",
              "bigdata-" + i
      ));
    }

    producer.close();
  }
}
